package com.dgut.sales.controller;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;

import com.dgut.sales.bean.Bills;
import com.dgut.sales.bean.Contract;
import com.dgut.sales.bean.Details;
import com.dgut.sales.bean.Goods;
import com.dgut.sales.bean.Purchase;
import com.dgut.sales.dynamic.sql.bean.ContractSearch;
import com.dgut.sales.service.BillsService;
import com.dgut.sales.service.ContractService;
import com.dgut.sales.service.CustomerService;
import com.dgut.sales.service.DetailsService;
import com.dgut.sales.service.GoodsService;

@Controller
@RequestMapping("/contract")
public class ContractController {
	
	@Autowired
	private ContractService service;
	@Autowired
	private DetailsService detailsService;
	@Autowired
	private BillsService billsService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private CustomerService customerService;
	
	@RequestMapping("/toPage/{pageName1}/{pageName2}")
	public String ReturnPage(@PathVariable("pageName1") String pageName1, @PathVariable("pageName2") String pageName2) {
		return pageName1 + "/" + pageName2 ;
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	} 
	
	@GetMapping("/list")
	public String list(Model model) {
		List<Contract> contracts = service.getAllContract();
		model.addAttribute("contracts", contracts);
		return "contract/contractList";
	}
	
	
	/*
	 * 通过合同id查出所有在此合同里的货物详情
	 * */
	@GetMapping ("/getDetails/{id}")
	public String getDetails(@PathVariable("id") Integer id, Model model) {
		List<Map<String, Details>> contractDetails = service.getContractDetails(id);
		model.addAttribute("contractDetails", contractDetails);
		Contract contract = service.getContractById(id);
		model.addAttribute("contract", contract);
		return"contract/details";
	}
	
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, Model model) {
		Contract contract = service.getContractById(id);
		if(!contract.getStates().equals("未履行")) 
			model.addAttribute("states", "不可修改");
		else {
			model.addAttribute("states", "可修改");
		}
		
		model.addAttribute("contract", contract);
		return "contract/contractEdit";
	}
	
	@ModelAttribute
	public void prepareModel(@RequestParam(value="contractId", required=false)Integer id, HttpServletRequest request, Model model) {
		String servletPath = request.getServletPath();
		if(servletPath.equals("/contract/update")) {
			Contract contract = service.getContractById(id);
			model.addAttribute("contract", contract);
		}
		if(servletPath.equals("/contract/addContract")) {
			model.addAttribute("contract", new Contract());
		}
		model.addAttribute("contractSearch", new ContractSearch());
	}
	
	@PutMapping("/update")
	public String update(Contract contract, HttpServletRequest request) {
		service.updateContract(contract);
		return "redirect:/contract/list";
	}
	
	@PostMapping("/input")
	public String input(Model model) {
		model.addAttribute("contract", new Contract());
		return "contract/inputContract";
	}
	
	
	@PostMapping("/addContract")
	public String addContract(@ModelAttribute Contract contract) {
		service.addContract(contract);
		return "redirect:/contract/list";
	}
	
	
	@GetMapping("/deleteContract/{id}")
	public String deleteContract(@PathVariable("id") Integer id) {
		
		service.delete(id);
		
		return "redirect:/contract/list";
	}
	@PostMapping("/inputCondition")
	public String inputCondition(Model model) {
		model.addAttribute("contractSearch", new ContractSearch());
		return "contract/search";
	}
	
	@PostMapping("/search")
	public String search(@ModelAttribute ContractSearch contractSearch, Model model) {
		List<Contract> contracts = service.searchContract(contractSearch);
		model.addAttribute("contracts", contracts);
		return "contract/searchResult";
	}
	
	/*
	 * id: 详情表的id
	 */
	@GetMapping("/buildBills/{id}")
	public String bulidBills(@PathVariable("id") Integer id,Model model) {
		Goods goods = goodsService.getGoodsById(detailsService.getDetailsById(id).getGoodsId());
		Integer contractId = detailsService.getDetailsById(id).getContractId();
		Integer shopNum = detailsService.getDetailsById(id).getShopNum();
		if(goods.getQuantity()>=shopNum) {
			detailsService.updateStates(id);
			String name = service.getContractById(contractId).getCustomer();
			String address = customerService.getCustomerByName(name).getAddress();
			Details details = detailsService.getDetailsById(id);
			Bills bills = new Bills(details.getId(), details.getGoodsId(), goods.getGoodsName(), goods.getPrice(),
					details.getShopNum(), details.getStates(), new Date(), details.getStates(), address);
			billsService.addBills(bills);
			Contract contract = service.getContractById(contractId);
			contract.setStates("履行中");
			service.updateContract(contract);
		}
		else {
			model.addAttribute("contractId", contractId);
			model.addAttribute("shopNum", shopNum);
			model.addAttribute("goods", goods);
			model.addAttribute("purchase", new Purchase());
			return "/bills/purchase";
		}
		return "redirect:/contract/getDetails/" + contractId;
	}
	
	

}
